package com.qf.controller;

import com.qf.pojo.entity.Student;
import com.qf.pojo.vo.StudentVO;
import com.qf.result.Result;
import com.qf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;


    //根据id查询学生信息
    @GetMapping("/find")
    public Result<Student> getStudent(int id) {
        Student student = studentService.findStudent(id);
        return Result.success(student);
    }

    //查询所有学生信息
    @GetMapping("/all")
    public Result<List<Student>> getAllStudent() {
        List<Student> list = studentService.findAllStudent();
        return Result.success(list);
    }

    //根据条件查询学生信息
    @GetMapping("/search")
    public Result<List<Student>> findAll(@RequestParam HashMap<String,Object> map){
        List<Student> list = studentService.findByKeyword(map);
        return Result.success(list);
    }

    //新增学生信息
    @PostMapping("/add")
    public Result<Integer> addStudent(@RequestBody Student student){
        studentService.addStudent(student);
        return Result.success();
    }

    //修改学生信息
    @PutMapping("/update")
    public Result<Integer> updateStudent(@RequestBody Student student){
        studentService.updateStudent(student);
        return Result.success();
    }

    //删除学生信息
    @DeleteMapping("/delete")
    public Result<Integer> deleteStudent(int id){
        studentService.deleteStudent(id);
        return Result.success();
    }

    //批量删除学生信息
    @DeleteMapping("/batchDelete")
    public Result<Integer> batchDeleteStudent(@RequestBody List<Integer> ids){
        studentService.batchDeleteStudent(ids);
        return Result.success();
    }

    //查询所有学生信息（包含班级信息）
    @GetMapping("/class")
    public Result<List<StudentVO>> findAllStudentAndClassroom() {
        List<StudentVO> list = studentService.findAllStudentAndClassroom();
        return Result.success(list);
    }

    // 根据id查询1个学生信息以及关联的所有学科信息
    @GetMapping("/subjects")
    public Result<StudentVO> findStudentAndAllSubjectByStuid(int sid){
        StudentVO studentVO = studentService.findStudentAndAllSubjectByStuid(sid);
        return Result.success(studentVO);
    }

    @GetMapping("/subjects/teacher")
    public Result<StudentVO> findStudentAndAllSubjectAndTeacherByStuid(int sid){
        StudentVO studentVO = studentService.findStudentAndAllSubjectAndTeacherByStuid(sid);
        return Result.success(studentVO);
    }
}
